package com.reekr.a5t.web.controller;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.activiti.engine.impl.interceptor.Command;
import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.reekr.a5t.model.activiti.DeploymentVo;
import com.reekr.a5t.model.activiti.ProcessInstanceVo;
import com.reekr.a5t.service.ActivitiService;
import com.reekr.a5t.service.acitiviti.ProcessInstanceDiagramCmd;
import com.reekr.a5t.web.annotation.SessionInfo;
import com.reekr.a5t.web.json.MyObjectMapper;

@SessionInfo
@RequestMapping("/act")
@Controller
public class ActivitiController {

	@Autowired
	ActivitiService activitiService;
	
	private JsonGenerator jsonGenerator = null;
    private MyObjectMapper objectMapper = null;
	
	@RequestMapping(value="/deploymentTop10.json",method=RequestMethod.GET)
	public void listDeploymentTop10(Model model)
	{
		List<DeploymentVo> list = activitiService.listDeploymentTop10();
		model.addAttribute("success", true);
		model.addAttribute("deployments", list);
		
	}
	
	@RequestMapping(value="/deployment.json",method=RequestMethod.GET)
	public void deployment(Model model)
	{
		activitiService.deployment();
		model.addAttribute("success", true);
	}
	
	@RequestMapping(value="/clearAll.json",method=RequestMethod.GET)
	public void clearAll(Model model)
	{
		activitiService.clearAll();
		model.addAttribute("success", true);
	}
	
	@RequestMapping(value="/initiate.json",method=RequestMethod.GET)
	public void initiate(Model model)
	{
		activitiService.initiate();
		model.addAttribute("success", true);
	}
	
	@RequestMapping(value="/listProcessInstance.json",method=RequestMethod.GET)
	public void listProcessInstance(Model model)
	{
		List<ProcessInstanceVo> pis = activitiService.listProcessInstance();
		model.addAttribute("success", true);
		model.addAttribute("pis", pis);
	}
	
	@RequestMapping(value = "/image")
	public ResponseEntity<byte []> image(@RequestParam("processInstanceId") String processInstanceId) throws MalformedURLException, IOException {
		InputStream in = activitiService.getImage(processInstanceId);
	    byte[] bb = IOUtils.toByteArray(in);
	    in.close();
	    HttpHeaders headers = new HttpHeaders();
	    headers.setContentType(MediaType.IMAGE_PNG);
	    headers.setCacheControl("no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
	    headers.setPragma("no-cache");
	    return new ResponseEntity<byte []>(bb, headers, HttpStatus.OK);
	}
}
